package com.example.fmgidsprototype.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;

import com.example.fmgidsprototype.data.CategoryTable.CategoryColumns;
import com.example.fmgidsprototype.data.VenueCategoryTable.VenueCategoryColumns;
import com.example.fmgidsprototype.data.VenueTable.VenueColumns;
import com.example.fmgidsprototype.model.Category;
import com.example.fmgidsprototype.model.Venue;

public class CategoryDao implements Dao<Category>{
	
	private SQLiteDatabase db;
	private SQLiteStatement insertStatement;
	private static final String INSERT = "insert or ignore into " + CategoryTable.TABLE_NAME + "(" +
			CategoryColumns.NAME + ", " + CategoryColumns.DESCRIPTION
			+ ", " + CategoryColumns.ICON_NAME + ") values (?,?,?)";
	
	public CategoryDao(SQLiteDatabase db){
		this.db = db;
		insertStatement = db.compileStatement(CategoryDao.INSERT);
	}

	@Override
	public long save(Category category) {
		insertStatement.clearBindings();
		insertStatement.bindString(1, category.getName());
		insertStatement.bindString(2, category.getDescription());
		insertStatement.bindString(3, category.getIconUrl());
		try{
			return insertStatement.executeInsert();
		}
		catch(Exception e){
			return -1;
		}
	}

	@Override
	public long update(Category category) {
		final ContentValues values = new ContentValues();
		values.put(CategoryColumns.NAME, category.getName());
		values.put(CategoryColumns.DESCRIPTION, category.getDescription());
		values.put(CategoryColumns.ICON_NAME, category.getIconUrl());
		try{
			return db.update(CategoryTable.TABLE_NAME, values, CategoryColumns.NAME + " = ?", 
					new String[]{category.getName()}); 
		}
		catch(Exception e){
			return -1;
		}
		
	}

	@Override
	public void delete(Category category) {
		if(category.getId() > 0){
			db.delete(CategoryTable.TABLE_NAME, BaseColumns._ID + " = ?", 
					new String[]{String.valueOf(category.getId())});
		}
	}
	
	public long findCategory(String name){
		long id = 0L;
		String query = "select _id from " + CategoryTable.TABLE_NAME + " where upper(" + CategoryColumns.NAME +
				") = ?";
		Cursor c = db.rawQuery(query, new String[]{name.toUpperCase()});
		if(c.moveToNext()){
			id = c.getLong(0);
		}
		return id;
	}

	@Override
	public Category get(long id) {
		Category category = null;
		Cursor c = db.query(CategoryTable.TABLE_NAME, new String[]{
				BaseColumns._ID, CategoryColumns.NAME, CategoryColumns.DESCRIPTION,
				CategoryColumns.ICON_NAME
		}, BaseColumns._ID + " = ?", new String[]{String.valueOf(id)}, null, null, null, "1");
		if(c.moveToFirst())
			createCategoryFromCursor(c);
		if(!c.isClosed())
			c.close();
		return category;
	}
	

	@Override
	public List<Category> getAll() {
		List<Category> list = new ArrayList<Category>();
		Cursor c = db.query(CategoryTable.TABLE_NAME, new String[]{
				BaseColumns._ID, CategoryColumns.NAME, CategoryColumns.DESCRIPTION,
				CategoryColumns.ICON_NAME
		}, null, null, null, null, null);
		if(c.moveToFirst()){
			do{
				Category category = createCategoryFromCursor(c);
				list.add(category);
			}
			while(c.moveToNext());
		}
		if(!c.isClosed())
			c.close();
		return list;
	}
	
	
	public static Category createCategoryFromCursor(Cursor c){
		Category category = null;
		if(c != null){
			category = new Category();
			category.setId(c.getLong(0));
			category.setName(c.getString(1));
			category.setDescription(c.getString(2));
			category.setIconUrl(c.getString(3));
		}
		return category;
	}

	public void deleteAll(){
		String deleteSql = "delete from " + CategoryTable.TABLE_NAME ;
		db.execSQL(deleteSql);
	}

}
